﻿Public Class frmCartonContrareembolsoCA
    Dim objRpt As New crCartonContrareembolso


    Private Sub btnNumeroATexto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNumeroATexto.Click
        Dim decimalPosition As Integer
        Dim entero As String
        Dim decim As String
        Dim enteroLetras As String
        Dim decimalLetras As String

        decimalPosition = InStr(txtValorDeclaradoNumero.Text, ".")
        If decimalPosition > 0 Then
            entero = txtValorDeclaradoNumero.Text.Substring(0, decimalPosition - 1)
            decim = txtValorDeclaradoNumero.Text.Substring(decimalPosition)
            If decim.Length > 2 Then
                decim = decim.Substring(0, 2)
            ElseIf decim.Length = 1 Then
                decim = decim + "0"
            End If
        Else
            entero = txtValorDeclaradoNumero.Text
            decim = 0
        End If

        enteroLetras = comunes.Num2Text(Val(entero))
        decimalLetras = IIf(decim > 0, comunes.Num2Text(Val(decim)), "")
        txtValorDeclaradoLetras.Text = (enteroLetras + IIf(decim > 0, " CON " + decimalLetras + " CENTAVOS", "")).ToString.ToLower
    End Sub

    Private Sub btnVerCarton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVerCarton.Click
        updateReport()

        objRpt.Section2.ReportObjects.Item("imgBackground").ObjectFormat.EnableSuppress = False

        frmCartonContrareembolsoCAViewer.crViewer.ReportSource = objRpt
        frmCartonContrareembolsoCAViewer.MdiParent = Me.MdiParent
        frmCartonContrareembolsoCAViewer.Show()
        frmCartonContrareembolsoCAViewer.crViewer.Refresh()
    End Sub

    Private Sub frmCartonContrareembolsoCA_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Public Sub updateReport()
        Dim ValorDeclaradoLetras As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim ValorDeclaradoNumeros As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim Remitente As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim OficinaImposicion As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim DomicilioOrigen As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim DomicilioDestino As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim DNI As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim Destinatario As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim CodigoPostalOficinaOrigen As CrystalDecisions.CrystalReports.Engine.TextObject
        Dim CodigoPostalDestinatario As CrystalDecisions.CrystalReports.Engine.TextObject



        ValorDeclaradoLetras = objRpt.ReportDefinition.ReportObjects("ValorDeclaradoLetras")
        ValorDeclaradoNumeros = objRpt.ReportDefinition.ReportObjects("ValorDeclaradoNumeros")
        Remitente = objRpt.ReportDefinition.ReportObjects("Remitente")
        OficinaImposicion = objRpt.ReportDefinition.ReportObjects("OficinaImposicion")
        DomicilioOrigen = objRpt.ReportDefinition.ReportObjects("DomicilioOrigen")
        DomicilioDestino = objRpt.ReportDefinition.ReportObjects("DomicilioDestino")
        DNI = objRpt.ReportDefinition.ReportObjects("DNI")
        Destinatario = objRpt.ReportDefinition.ReportObjects("Destinatario")
        CodigoPostalOficinaOrigen = objRpt.ReportDefinition.ReportObjects("CodigoPostalOficinaOrigen")
        CodigoPostalDestinatario = objRpt.ReportDefinition.ReportObjects("CodigoPostalDestinatario")

        CodigoPostalOficinaOrigen.Text = txtCodigoPostalOficina.Text
        CodigoPostalDestinatario.Text = txtCodigoPostal.Text
        ValorDeclaradoLetras.Text = txtValorDeclaradoLetras.Text
        ValorDeclaradoNumeros.Text = txtValorDeclaradoNumero.Text
        Destinatario.Text = txtDestinatarioNombre.Text & " " & txtDestinatarioApellido.Text
        DomicilioDestino.Text = txtDestinatarioDireccion.Text & IIf(txtDestinatarioLocalidad.Text <> "", ", " & txtDestinatarioLocalidad.Text, "") & IIf(txtDestinatarioProvincia.Text <> "", ", " & txtDestinatarioProvincia.Text, "")
        Remitente.Text = txtRemitenteNombre.Text & " " & txtRemitenteApellido.Text
        DomicilioOrigen.Text = txtRemitenteDireccion.Text & " (" & txtRemitenteCodigoPostal.Text & ")" & IIf(txtRemitenteLocalidad.Text <> "", ", " & txtRemitenteLocalidad.Text, "") & IIf(txtRemitenteProvincia.Text <> "", ", " & txtRemitenteProvincia.Text, "")
        DNI.Text = "DNI: " & txtRemitenteDni.Text
        OficinaImposicion.Text = txtOficina.Text

    End Sub

    Private Sub btnImprimir_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnImprimir.Click
        updateReport()
        objRpt.Section2.ReportObjects.Item("imgBackground").ObjectFormat.EnableSuppress = True

        printDialog.Document = printDocument
        Dim dr As DialogResult = printDialog.ShowDialog()

        If dr = DialogResult.OK Then
            Dim nCopy As Integer = printDocument.PrinterSettings.Copies
            Dim sPage As Integer = printDocument.PrinterSettings.FromPage
            Dim ePage As Integer = printDocument.PrinterSettings.ToPage
            Dim printerName As String = printDocument.PrinterSettings.PrinterName

            Try
                objRpt.PrintOptions.PrinterName = printerName
                objRpt.PrintToPrinter(nCopy, False, sPage, ePage)
            Catch ex As Exception
                MessageBox.Show(ex.ToString())
            End Try
        End If

    End Sub

End Class

